// pages/dingdan/dingdan.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dingdannav: [{ id: '0', tit: '全部' }, { id: '1', tit: '待付款' }, { id: '2', tit: '待发货' }, { id: '3', tit: '待收货' }, { id: '4', tit: '待评价' }],
    current:0,
    order:[],
    showModalStatus:false,
    pstype:'1',
    types: [
      { id: '1', img: "/images/weixin.png", typeName: "微信支付", checked: "true" },
      { id: "2", img: "/images/xxzf.png", typeName: "余额支付", }
    ],
    sn:"",
    stats: ['待付款', '已付款', '已发货', '订单完成', '退款完成订单关闭', '退款中', '订单关闭', '订单取消', '退款中', '待领取']
  },
  navtap:function(e){
    this.setData({
      current:e.currentTarget.id
    })
  },
  /*取消订单 */
  refund:function(e){
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/order/close',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      method:"POST",
      data:{
        id: e.currentTarget.dataset.sn
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          if(res.data.msg=='success'){
            wx.showToast({
              title: '取消订单成功',
              icon: 'successs',
              duration: 2000,
            })
            that.demand('1');
          }else{
            wx.showToast({
              title: res.data.msg ,
              icon: 'none',
              duration: 2000,
            })
          }
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  // 待付款
  toBuy:function(e){
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)

    var sn = e.currentTarget.dataset.sn
    this.setData({
      sn:sn
    })
    // console.log(sn,11111)
  },
  /*付款 */
  buy:function(e){
    var that=this;
    var pstype = that.data.pstype
    var sn = that.data.sn
    // console.log(sn,22222222)
    // console.log(pstype,33333)
    // console.log(e)
    wx.request({
      url: app.globalData.directory + 'api/v2/pay/order_sn/' + sn,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      data:{type:pstype},
      success: function (res) {
        console.log(res,"0000000000")
        if (pstype == "1") {
          if (res.data.status == 'ok') {
            wx.requestPayment({
              'timeStamp': res.data.data.timestamp,
              'nonceStr': res.data.data.nonceStr,
              'package': res.data.data.package,
              'signType': res.data.data.signType,
              'paySign': res.data.data.paySign,
              'success': function (res) {
                that.setData({
                  current:2,
                  sn:sn
                })
              },
              'fail': function (res) {
                wx.showToast({
                  title:res.errMsg,
                  icon: 'none',
                  duration: 2000,
                })
              }
            })
          } else {
            wx.showToast({
              title: '请求失败,请重试',
              icon: 'none',
              duration: 2000,
            })
          }
        } else {
          console.log(res.data.status,"******")
          console.log(res.data.msg, "/////")
          if (res.data.status == "ok") {
            if (res.data.msg == "success") {
              wx.request({
                url: app.globalData.directory + 'api/v2/user/getuser',
                method: "POST",
                data: that.data.order,
                header: {
                  'content-type': 'application/json',
                  'token': wx.getStorageSync('token')
                },
                success: function (res) {
                  console.log(res)
                  console.log(res.data.status,89898989)
                  if (res.data.status == "ok") {
                    wx.showToast({
                      title: '支付成功',
                      icon: 'success',
                      duration: 1000,
                    })
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '../dingdan/dingdan?id=2',
                      })
                    }, 1000)
                  }
                }
              })
            }
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000,
            })
            wx.showModal({
              title: '提示',
              content: '余额不足，是否去充值',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../toRecharge/toRecharge',
                  })
                } else {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '../dingdan/dingdan?id=1',
                    })
                  }, 1000)
                }
              }
            })
          }
        }
      }
    })
    that.hideModel()
  },
  conswiper:function(e){
    this.setData({
      current: e.detail.current
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    this.demand(e.detail.current);
  },
  /*再来一单 */
  order1:function(){
    wx.switchTab({
      url: '../order/order'
    })
  },
  /*评价 */
  pj: function (e) {
    wx.navigateTo({
      url: '../to-evaluate/to-evaluate?id='+e.currentTarget.dataset.id,
    })
  },
  bSure:function(e){
    console.log(e)
    var sn2 = e.currentTarget.dataset.sn
    console.log(sn2)
    var that = this
    wx.request({
      url: app.globalData.directory + 'api/v2/order/confirmOrder',
      data:{sn:sn2},
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success:function(res) {
         if(res.data.status =="ok"){
           wx.showToast({
             title: "处理订单成功",
             icon: 'success',
             duration: 2000,
           })
           setTimeout(function () {
             wx.redirectTo({
               url: '../dingdan/dingdan?id=4',
             })
           }, 1000)
         } else{
           wx.showToast({
             title: "处理失败",
             icon: 'none',
             duration: 2000,
           })
         }
      }
    })
  },
  demand:function(id){
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/order/getlist/' + id,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        // console.log("demand",res)
        if (res.data.status == 'ok') {
          wx.hideLoading();
          that.setData({
            order: res.data.data,
          })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  hideModel:function(){
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  radioChange: function (e) {
    this.setData({
      pstype: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    this.setData({
      current:options.id,
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    that.demand(options.id);
    // wx.removeStorageSync('shopcar')
    // wx.removeStorageSync('cart')
    // wx.removeStorageSync('cart1')
    // wx.setStorageSync('shopcar', [])
    // wx.setStorageSync('cart', [])
    // wx.setStorageSync('cart1', [])
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})